package jp.co.systemi.asp.workflow.user;

import java.util.List;
import java.util.logging.Logger;

import jp.co.systemi.asp.utils.DModalWindow;
import jp.co.systemi.asp.workflow.ICallback;
import jp.co.systemi.asp.workflow.Main;
import jp.co.systemi.asp.workflow.data.User;
import jp.co.systemi.asp.workflow.peer.UserPeer;

import org.apache.wicket.PageParameters;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.Radio;
import org.apache.wicket.markup.html.form.RadioGroup;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.PropertyModel;
import org.datanucleus.util.StringUtils;

public class UserSelect extends WebPage {
    @SuppressWarnings("unused")
    private static final Logger log = Logger.getLogger(UserSelect.class.getName());
    /** フィードバック */
    protected FeedbackPanel feedback = new FeedbackPanel("wi_feedback");
    RadioGroup<String> radioGroup = new RadioGroup<String>("wi_radioGroup", new PropertyModel<String>(this, "loginId"));

    private String loginId;
    public UserSelect(final Main mainPage, final DModalWindow window) {
        init(mainPage, window);
    }

    public UserSelect(final Main mainPage, final DModalWindow window, PageParameters pars) {
        init(mainPage, window);
    }

    @SuppressWarnings("unchecked")
    public void init(final Main mainPage, final DModalWindow window) {
        // デフォルト設定
        loginId = (String)((ICallback)mainPage).getDefault(null);
        feedback.setOutputMarkupId(true);
        add(feedback);

        final List<User> users = UserPeer.doSearch(null);

        Form form = new Form("wi_form");
        this.add(form);
        form.add(new AjaxButton("wi_OK") {
            private static final long serialVersionUID = 5873629255088464587L;
            @Override
            protected void onSubmit(AjaxRequestTarget target, Form<?> form) {

                PageParameters pars = new PageParameters();
                pars.add("user.select", "OK");
                String msg = ((ICallback)mainPage).getEmptyMessage(pars);
                if(StringUtils.isEmpty(loginId) && !StringUtils.isEmpty(msg)){
                    error(msg);
                    target.addComponent(feedback);
                    return;
                }
                pars.add("loginId", loginId);
                for(User user : users){
                    if(user.getLoginId().equals(loginId)){
                        pars.add("userName", UserPeer.getName(user));
                        break;
                    }
                }
                ((ICallback)mainPage).setResult(pars);
                window.setWindowClosedCallback(window.getOKActionCallback());
                window.close(target);
            }

        });
        form.add(new AjaxButton("wi_Cancel") {
            private static final long serialVersionUID = 5873629255088464587L;
            @Override
            protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
                window.setWindowClosedCallback(window.getCancelActionCallback());
                window.close(target);
            }
        });

        form.add(radioGroup);
        radioGroup.add(new ListView<User>("wi_trUser", users){
            private static final long serialVersionUID = 1L;

            @Override
            protected void populateItem(ListItem<User> item) {
                User user = (User)item.getModelObject();
                Radio<String> radio = new Radio<String>("wi_radio", new PropertyModel<String>(user, "loginId"));
                radioGroup.add(radio);
                item.add(radio);
                item.add(new Label("wi_lastName", new PropertyModel<String>(user, "lastName")));
                item.add(new Label("wi_firstName", new PropertyModel<String>(user, "firstName")));
                item.add(new Label("wi_email", new PropertyModel<String>(user, "loginId")));
                item.add(new Label("wi_groupName", new PropertyModel<String>(user, "groupName")));

            }

        });
    }
}
